<?php
namespace app\index\controller;
use app\BaseController;
use app\Request;
use think\facade\Db;
class Inquiry extends BaseController
{

    public function index(){
        $http = GetHttp().$_SERVER['SERVER_NAME'];
        $ip = get_client_ip();
        $useragent = addslashes($_SERVER['HTTP_USER_AGENT']);
        if(strpos($useragent, 'MicroMessenger') === false && strpos($useragent, 'Windows Phone') === false ){
            $cs = "?ip=$ip&opentime=".urlencode(md5(time()));
            $url =  $http."/inquiry/inquiry/inquiry.html".$cs;
        }else{
            echo "<div style='width: 80%;margin: 300px auto 0 auto;'><img src='/assets/img/llqydt1.jpg' style='width: 100%; height: 61%;'/></div>";die;
        }
        header("Location: $url");exit();
    }

    public function inquiry(){
//        $wq = array(
//            'water' => array('name'=>'喝水情况','A'=>'232323'),
//            'water1' => array('name'=>'喝水情况','A'=>'232323')
//        );
//        $this -> assign('wq',$wq);
        return $this -> fetch();

    }

    public function save(){

        $data = [
            'username' => $_POST['username'],
            'mobile' => $_POST['mobile'],
            'sex' => $_POST['sex'],
            'age' => $_POST['age'],
            'address'=>$_POST['address'],
            'ip' => get_client_ip(),
            'status' => 1,
            'createtime'=>time()
        ];

        $inquiry = Db::name('inquiry') -> where('mobile',$_POST['mobile']) -> value('id');
        if(empty($inquiry)){
            $resid = Db::name('inquiry') -> insertGetId($data);
        }else{
            $resid = $inquiry;
        }
        $data1 = [
            'inquiryid' => $resid,
            'mobile' => $_POST['mobile'],
            'water' => $_POST['water'],
            'appetite' => $_POST['appetite'],
            'stool' => $_POST['stool'],
            'stoolcolor' => $_POST['stoolcolor'],
            'urinate'=>$_POST['urinate'],
            'urinatecolor' => $_POST['urinatecolor'],
            'sleepcondition' => $_POST['sleepcondition'],
            'sleepsymptoms' => $_POST['sleepsymptoms'],
            'mood' => $_POST['mood'],
            'bodyg'=>$_POST['bodyg'],
            'sweating' => $_POST['sweating'],
            'headpharynx' => $_POST['headpharynx'],
            'cough' => $_POST['cough'],
            'abdomen' => $_POST['abdomen'],
            'health'=>$_POST['health'],
            'health1'=>$_POST['health1'],
            'health2' => $_POST['health2'],
            'health3' => $_POST['health3'],
            'health4' => $_POST['health4'],
            'health5' => $_POST['health5'],
            'content' => $_POST['content'],
            'inimages' => $_POST['inimages'],
            'createtime'=>time()
        ];
        $inquiry_logid = Db::name('inquiry_log') -> insertGetId($data1);
        if($inquiry_logid){
            $dataid = [
                'inquiryid' => encrypt($resid,'E','inquiry'),
                'inquiryinfoid'=>encrypt($inquiry_logid,'E','inquiry')
            ];
//        encrypt('str','E','nowamagic');
            echo  apisucess('ok',$dataid );die;
        }

    }


    public function details(){
        $inquiryid = encrypt($_GET['inquiryid'],'D','inquiry');
        $inquiryinfoid = encrypt($_GET['inquiryinfoid'],'D','inquiry');
        $binfo = Db::name('inquiry') -> where('id',$inquiryid)  -> find();
        if($binfo['sex'] == 1){
            $binfo['sex'] = '男';
        }else if($binfo['sex'] == 2){
            $binfo['sex'] = '女';
        }else{
            $binfo['sex'] = '';
        }
        $info = Db::name('inquiry_log') -> where(['inquiryid'=>$inquiryid,'id'=>$inquiryinfoid]) -> find() ;
        $info['images'] = explode(',',$info['inimages']);
        $this -> assign('binfo',$binfo);
        $this -> assign('info',$info);
        return $this -> fetch();
    }
}